import request from '@/utils/request'

// ==================== 考试管理 ====================

// 获取考试列表
export function getExamPage(params) {
  return request({
    url: '/v1/exams',
    method: 'get',
    params
  })
}

// 获取考试详情
export function getExamDetail(id) {
  return request({
    url: `/v1/exams/${id}`,
    method: 'get'
  })
}

// 创建考试
export function createExam(data) {
  return request({
    url: '/v1/exams',
    method: 'post',
    data
  })
}

// 更新考试
export function updateExam(id, data) {
  return request({
    url: `/v1/exams/${id}`,
    method: 'put',
    data
  })
}

// 删除考试
export function deleteExam(id) {
  return request({
    url: `/v1/exams/${id}`,
    method: 'delete'
  })
}

// 发布考试
export function publishExam(id) {
  return request({
    url: `/v1/exams/${id}/publish`,
    method: 'post'
  })
}

// 暂停考试
export function pauseExam(id) {
  return request({
    url: `/v1/exams/${id}/pause`,
    method: 'post'
  })
}

// 恢复考试
export function resumeExam(id) {
  return request({
    url: `/v1/exams/${id}/resume`,
    method: 'post'
  })
}

// 结束考试
export function endExam(id) {
  return request({
    url: `/v1/exams/${id}/end`,
    method: 'post'
  })
}

// 复制考试
export function copyExam(id, title) {
  return request({
    url: `/v1/exams/${id}/copy`,
    method: 'post',
    params: { title }
  })
}

// 审核考试
export function reviewExam(id, approved, comment) {
  return request({
    url: `/v1/exams/${id}/review`,
    method: 'post',
    params: { approved, comment }
  })
}

// 获取考试统计
export function getExamStatistics(id) {
  return request({
    url: `/v1/exams/${id}/statistics`,
    method: 'get'
  })
}

// 获取关联考试
export function getRelatedExams(entityType, entityId, limit = 10) {
  return request({
    url: '/v1/exams/related',
    method: 'get',
    params: { entityType, entityId, limit }
  })
}

// 更新考试状态
export function updateExamStatus(id, status) {
  return request({
    url: `/v1/exams/${id}/status`,
    method: 'put',
    data: { status }
  })
}

// 获取课程列表（用于考试表单）
export function getCourseList(params) {
  return request({
    url: '/v1/courses',
    method: 'get',
    params
  })
}

// ==================== 题目管理 ====================

// 获取题目列表
export function getQuestionPage(params) {
  return request({
    url: '/v1/questions',
    method: 'get',
    params
  })
}

// 获取题目详情
export function getQuestionDetail(id) {
  return request({
    url: `/v1/questions/${id}`,
    method: 'get'
  })
}

// 创建题目
export function createQuestion(data) {
  return request({
    url: '/v1/questions',
    method: 'post',
    data
  })
}

// 更新题目
export function updateQuestion(id, data) {
  return request({
    url: `/v1/questions/${id}`,
    method: 'put',
    data
  })
}

// 删除题目
export function deleteQuestion(id) {
  return request({
    url: `/v1/questions/${id}`,
    method: 'delete'
  })
}

// 批量导入题目
export function importQuestions(data) {
  return request({
    url: '/v1/questions/import',
    method: 'post',
    data
  })
}

// 导出题目
export function exportQuestions(params) {
  return request({
    url: '/v1/questions/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// ==================== 考试记录管理 ====================

// 获取考试记录列表
export function getExamRecordPage(params) {
  return request({
    url: '/v1/exam-records',
    method: 'get',
    params
  })
}

// 获取考试记录详情
export function getExamRecordDetail(id) {
  return request({
    url: `/v1/exam-records/${id}`,
    method: 'get'
  })
}

// 开始考试
export function startExam(data) {
  return request({
    url: '/v1/exam-records/start',
    method: 'post',
    data
  })
}

// 提交考试
export function submitExam(data) {
  return request({
    url: '/v1/exam-records/submit',
    method: 'post',
    data
  })
}

// 获取考试统计报告
export function getExamReport(examId, params) {
  return request({
    url: `/v1/exam-records/report/${examId}`,
    method: 'get',
    params
  })
}

// 导出考试记录
export function exportExamRecords(params) {
  return request({
    url: '/v1/exam-records/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}
